fix(cloudflare): Try/catch on non-configurable prototypes - #23132
Conversation
| try { | ||
| Object.defineProperty(prototype, methodName, { ...descriptor, value: wrapped }); | ||
| } catch {} | ||
|
|
||
| // Only the wrapper is marked, not the original method: `wrapMethodWithSentry` resolves | ||
| // through the same global map and must not resolve the original to this wrapper, | ||
| // which would recurse. |
There was a problem hiding this comment.
Bug: The code repeatedly tries to instrument non-configurable methods on each new class instance because it doesn't mark them as "failed to wrap" after the first attempt.
Severity: LOW
Suggested Fix
To prevent redundant work, the original method should be marked as having had an instrumentation attempt, even if it fails. This could be achieved by marking the original descriptor.value within the catch block after Object.defineProperty throws, preventing getRpcMethodDescriptor from re-selecting it for wrapping on subsequent constructions.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: packages/cloudflare/src/durableobject.ts#L331-L337
Potential issue: When a class has a non-configurable method, the instrumentation logic
attempts to wrap it using `Object.defineProperty`. This fails as expected, but the
original method is not marked to prevent future wrapping attempts. Consequently, for
every new instance of the class, the system re-attempts the same failed wrapping
operation. This results in minor performance overhead due to the creation of a new
closure and a doomed `defineProperty` call on each construction. The application's
functionality is not affected, but it introduces redundant work.
Did we get this right? 👍 / 👎 to inform future reviews.
size-limit report 📦
|
Missed a comment here: #23057 (comment)